﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Plumbing.Properties;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class FormSetSanitary : System.Windows.Forms.Form
	{
		public SetSanitaryData SanitaryData
		{
			get
			{
				return this.setSanitaryData;
			}
			set
			{
				this.setSanitaryData = value;
			}
		}

		public FormSetSanitary(SetSanitaryData setSanitaryData, Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			//base.Icon = Resource.YArchitechIcon;
			this.sqlHelper = new SQLiteDataBaseHelper(Path.Combine(Product.DataLocation, "HYBIMSPACE.db3"));
			this.supplyTerminalList = HYCalSQLData.GetSupplyTerminal();
			this.setSanitaryData = setSanitaryData;
			this.InitData();
			this.InitParameterSetting();
		}

		private void FormSetSanitary_Load(object sender, EventArgs e)
		{
			if ((int)this.doc.ActiveView.ViewType == 4)
			{
				this.btnColdPoint.Enabled = false;
				this.btnHotPoint.Enabled = false;
			}
			else
			{
				this.btnColdPoint.Enabled = true;
				this.btnHotPoint.Enabled = true;
			}
			if (this.setSanitaryData.HotPointVector == null || this.setSanitaryData.HotPointVector.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0)))
			{
				this.btnHotPoint.Image = YArchitech.Plumbing.Properties.Resources.noselect;
			}
			else
			{
				this.btnHotPoint.Image = YArchitech.Plumbing.Properties.Resources.select;
			}
			if (this.setSanitaryData.ColdPointVector == null || this.setSanitaryData.ColdPointVector.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0)))
			{
				this.btnColdPoint.Image = YArchitech.Plumbing.Properties.Resources.noselect;
				return;
			}
			this.btnColdPoint.Image = YArchitech.Plumbing.Properties.Resources.select;
		}

		private void InitData()
		{
			this.cboTrap.Items.AddRange(new object[]
			{
				"无",
				"S弯",
				"P弯"
			});
			DataTable tableSchema = this.sqlHelper.GetTableSchema("SanitaryData");
			if (tableSchema != null && tableSchema.Rows.Count > 0)
			{
				foreach (object obj in tableSchema.Rows)
				{
					DataRow dataRow = (DataRow)obj;
					SetSanitaryData setSanitaryData = new SetSanitaryData();
					setSanitaryData.SanitaryType = dataRow["Name"].ToString();
					setSanitaryData.PointOffset = double.Parse(dataRow["PointOffset"].ToString());
					setSanitaryData.ColdOffset = double.Parse(dataRow["ColdOffset"].ToString());
					setSanitaryData.HotOffset = double.Parse(dataRow["HotOffset"].ToString());
					setSanitaryData.Trap = int.Parse(dataRow["Trap"].ToString());
					setSanitaryData.DrainEquivalent = double.Parse(dataRow["DrainEquivalent"].ToString());
					setSanitaryData.DrainFlow = double.Parse(dataRow["DrainFlow"].ToString());
					setSanitaryData.FeedEquivalent = double.Parse(dataRow["FeedEquivalent"].ToString());
					setSanitaryData.FeedFlow = double.Parse(dataRow["FeedFlow"].ToString());
					setSanitaryData.ShortName = dataRow["ShortName"].ToString();
					setSanitaryData.SupplyTerminalList = dataRow["SupplyTerminalId"].ToString();
					this.sanitaryDataList.Add(setSanitaryData);
					this.lstSanitary.Items.Add(setSanitaryData.SanitaryType);
				}
			}
		}

		private void InitParameterSetting()
		{
			if (this.setSanitaryData == null)
			{
				this.setSanitaryData = this.sanitaryDataList[0];
				this.hyTxtColdOffset.ForeColor = System.Drawing.Color.Red;
				this.hyTxtDrainEquivalent.ForeColor = System.Drawing.Color.Red;
				this.hyTxtDrainFlow.ForeColor = System.Drawing.Color.Red;
				this.hyTxtFeedEquivalent.ForeColor = System.Drawing.Color.Red;
				this.hyTxtFeedFlow.ForeColor = System.Drawing.Color.Red;
				this.hyTxtHotOffset.ForeColor = System.Drawing.Color.Red;
				this.hyTxtPointOffset.ForeColor = System.Drawing.Color.Red;
				this.hyTxtShortName.ForeColor = System.Drawing.Color.Red;
				this.cboTrap.ForeColor = System.Drawing.Color.Red;
			}
			else
			{
				foreach (SetSanitaryData setSanitaryData in this.sanitaryDataList)
				{
					if (this.setSanitaryData.SanitaryType == setSanitaryData.SanitaryType)
					{
						this.setSanitaryData.SupplyTerminalList = setSanitaryData.SupplyTerminalList;
						break;
					}
				}
			}
			this.SetData(this.setSanitaryData);
		}

		private void SetData(SetSanitaryData sanitaryData)
		{
			this.lstSanitary.SelectedItem = sanitaryData.SanitaryType;
			this.hyTxtPointOffset.Text = sanitaryData.PointOffset.ToString();
			this.hyTxtColdOffset.Text = sanitaryData.ColdOffset.ToString();
			this.hyTxtHotOffset.Text = sanitaryData.HotOffset.ToString();
			this.cboTrap.SelectedIndex = sanitaryData.Trap;
			this.hyTxtDrainEquivalent.Text = sanitaryData.DrainEquivalent.ToString();
			this.hyTxtDrainFlow.Text = sanitaryData.DrainFlow.ToString();
			this.hyTxtFeedEquivalent.Text = sanitaryData.FeedEquivalent.ToString();
			this.hyTxtFeedFlow.Text = sanitaryData.FeedFlow.ToString();
			if (sanitaryData.ShortName == null)
			{
				this.hyTxtShortName.Text = this.sanitaryDataList[this.lstSanitary.SelectedIndex].ShortName;
			}
			else
			{
				this.hyTxtShortName.Text = sanitaryData.ShortName;
			}
			this.RefreshSupplyTerminal(ref sanitaryData);
		}

		private void lstSanitary_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SetData(this.sanitaryDataList[this.lstSanitary.SelectedIndex]);
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.hyTxtColdOffset.Text == "" || this.hyTxtDrainEquivalent.Text == "" || this.hyTxtDrainFlow.Text == "" || this.hyTxtFeedEquivalent.Text == "" || this.hyTxtFeedFlow.Text == "" || this.hyTxtHotOffset.Text == "" || this.hyTxtPointOffset.Text == "" || this.hyTxtShortName.Text == "")
			{
				YJKMessageBox.Show("参数不能为空");
				return;
			}
			this.SanitaryData.SanitaryType = this.lstSanitary.SelectedItem.ToString();
			this.SanitaryData.PointOffset = double.Parse(this.hyTxtPointOffset.Text);
			this.SanitaryData.ColdOffset = double.Parse(this.hyTxtColdOffset.Text);
			this.SanitaryData.HotOffset = double.Parse(this.hyTxtHotOffset.Text);
			this.SanitaryData.Trap = this.cboTrap.SelectedIndex;
			this.SanitaryData.DrainEquivalent = double.Parse(this.hyTxtDrainEquivalent.Text);
			this.SanitaryData.DrainFlow = double.Parse(this.hyTxtDrainFlow.Text);
			this.SanitaryData.FeedEquivalent = double.Parse(this.hyTxtFeedEquivalent.Text);
			this.SanitaryData.FeedFlow = double.Parse(this.hyTxtFeedFlow.Text);
			this.SanitaryData.ShortName = this.hyTxtShortName.Text;
			SupplyTerminal supplyTerminal = this.picSuply.Tag as SupplyTerminal;
			if (supplyTerminal != null)
			{
				this.SanitaryData.KeyTerminal = supplyTerminal.KeyWord;
				this.SanitaryData.TerminalLegendName = this.GetTerminalLegendName(supplyTerminal.KeyWord);
			}
			this.UpdateSql();
			base.DialogResult = DialogResult.OK;
		}

		protected string GetTerminalLegendName(string shortName)
		{
			string result = string.Empty;
			if (this.supplyTerminalList == null || this.supplyTerminalList.Count == 0)
			{
				this.supplyTerminalList = HYCalSQLData.GetSupplyTerminal();
			}
			foreach (SupplyTerminal supplyTerminal in this.supplyTerminalList)
			{
				if (supplyTerminal.KeyWord == shortName)
				{
					result = supplyTerminal.Name;
					break;
				}
			}
			return result;
		}

		private bool UpdateSql()
		{
			string sqlcondition = string.Concat(new object[]
			{
				SanitaryPara.PointOffset,
				"='",
				this.SanitaryData.PointOffset,
				"',",
				SanitaryPara.ColdOffset,
				"='",
				this.SanitaryData.ColdOffset,
				"',",
				SanitaryPara.HotOffset,
				"='",
				this.SanitaryData.HotOffset,
				"',",
				SanitaryPara.Trap,
				"='",
				this.SanitaryData.Trap,
				"',",
				SanitaryPara.DrainEquivalent,
				"='",
				this.SanitaryData.DrainEquivalent,
				"',",
				SanitaryPara.DrainFlow,
				"='",
				this.SanitaryData.DrainFlow,
				"',",
				SanitaryPara.FeedEquivalent,
				"='",
				this.SanitaryData.FeedEquivalent,
				"',",
				SanitaryPara.FeedFlow,
				"='",
				this.SanitaryData.FeedFlow,
				"',",
				SanitaryPara.ShortName,
				"='",
				this.SanitaryData.ShortName,
				"'"
			});
			return this.sqlHelper.SetInfoById("SanitaryData", this.lstSanitary.SelectedIndex + 1, sqlcondition);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnChoose_Click(object sender, EventArgs e)
		{
		}

		private void btnHotPoint_Click(object sender, EventArgs e)
		{
			this.nChoose = 1;
			base.DialogResult = DialogResult.Abort;
		}

		private void btnColdPoint_Click(object sender, EventArgs e)
		{
			this.nChoose = 2;
			base.DialogResult = DialogResult.Abort;
		}

		private void btnFeed_Click(object sender, EventArgs e)
		{
			FormFeedEquivalent formFeedEquivalent = new FormFeedEquivalent();
			formFeedEquivalent.ShowDialog();
			if (formFeedEquivalent.DialogResult == DialogResult.OK)
			{
				this.hyTxtFeedFlow.Text = formFeedEquivalent.DFeedFlow.ToString("0.00");
				this.hyTxtFeedEquivalent.Text = formFeedEquivalent.DEquivalent.ToString("0.00");
			}
		}

		private void btnDrain_Click(object sender, EventArgs e)
		{
			FormDrainEquivalent formDrainEquivalent = new FormDrainEquivalent();
			formDrainEquivalent.ShowDialog();
			if (formDrainEquivalent.DialogResult == DialogResult.OK)
			{
				this.hyTxtDrainFlow.Text = formDrainEquivalent.DDrainageFlow.ToString("0.00");
				this.hyTxtDrainEquivalent.Text = formDrainEquivalent.DEquivalent.ToString("0.00");
			}
		}

		private void cboTrap_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = true;
		}

		private void RefreshSupplyTerminal(ref SetSanitaryData sanitaryData)
		{
			int num = -1;
			if (string.IsNullOrEmpty(sanitaryData.SupplyTerminalList))
			{
				return;
			}
			string[] array = sanitaryData.SupplyTerminalList.Split(new char[]
			{
				','
			});
			for (int i = 0; i < array.Length; i++)
			{
				int num2 = int.Parse(array[i]);
				if (num2 > 0)
				{
					foreach (SupplyTerminal supplyTerminal in this.supplyTerminalList)
					{
						if (File.Exists(supplyTerminal.Image) && supplyTerminal.Id == num2)
						{
							Bitmap image = new Bitmap(Image.FromFile(supplyTerminal.Image), this.picSuply.Width, this.picSuply.Height);
							if (num == -1)
							{
								this.picSuply.Image = image;
								this.picSuply.Tag = supplyTerminal;
								num = 0;
							}
							if (sanitaryData.KeyTerminal == supplyTerminal.KeyWord)
							{
								this.picSuply.Image = image;
								this.picSuply.Tag = supplyTerminal;
								break;
							}
							break;
						}
					}
				}
			}
		}

		private void picSuply_Click(object sender, EventArgs e)
		{
			SupplyTerminal supplyTerminal = this.picSuply.Tag as SupplyTerminal;
			FormSetSanitaryList formSetSanitaryList = new FormSetSanitaryList(supplyTerminal, this.sanitaryDataList[this.lstSanitary.SelectedIndex], this.supplyTerminalList);
			formSetSanitaryList.Owner = this;
			if (formSetSanitaryList.ShowDialog() == DialogResult.OK && supplyTerminal != null && supplyTerminal.KeyWord != formSetSanitaryList.selSup.KeyWord)
			{
				Bitmap image = new Bitmap(Image.FromFile(formSetSanitaryList.selSup.Image), this.picSuply.Width, this.picSuply.Height);
				this.picSuply.Image = image;
				this.picSuply.Tag = formSetSanitaryList.selSup;
			}
		}

		private const string tableName = "SanitaryData";

		private SQLiteDataBaseHelper sqlHelper;

		private SetSanitaryData setSanitaryData = new SetSanitaryData();

		private List<SetSanitaryData> sanitaryDataList = new List<SetSanitaryData>();

		private List<SupplyTerminal> supplyTerminalList;

		public int nChoose;

		private Document doc;
	}
}
